package com.hellobike.haxing.modules.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hellobike.haxing.modules.purchase.common.TimerUtil;
import com.hellobike.haxing.modules.purchase.entity.TPurchaseProRecord;
import com.hellobike.haxing.modules.purchase.mapper.TPurchaseProRecordMapper;
import com.hellobike.haxing.modules.purchase.service.ITPurchaseProRecordService;
import com.hellobike.haxing.modules.wms.utils.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: t_purchase_pro_record
 * @Author: haxing-boot
 * @Date:   2020-03-04
 * @Version: V1.0
 */
@Service
public class TPurchaseProRecordServiceImpl extends ServiceImpl<TPurchaseProRecordMapper, TPurchaseProRecord> implements ITPurchaseProRecordService {

    @Autowired
    private TPurchaseProRecordMapper tPurchaseProRecordMapper;

    @Override
    public List<TPurchaseProRecord> getRecord(TPurchaseProRecord query) {
        List<TPurchaseProRecord> tPurchaseProSubRecords = new ArrayList<>();
        if(query.getSupplierId() == null){
            return tPurchaseProSubRecords;
        }
        QueryWrapper<TPurchaseProRecord> queryWrapper = new QueryWrapper<>();
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        String nowTime = format1.format(new Date());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<TPurchaseProRecord> tPurchaseProRecords = new ArrayList<>();
        try {
            Date timeLast23 = format.parse(TimerUtil.getLast23Date());
            Date time14 = format.parse(nowTime + " 14:00:00");
            Date time20 = format.parse(nowTime + " 20:00:00");
            Date time23 = format.parse(TimerUtil.getcurrent23Date());
            if(new Date().after(timeLast23) && new Date().before(time14)){
                return tPurchaseProSubRecords;
            }else if(new Date().after(time14) && new Date().before(time20)) {
                tPurchaseProRecords = tPurchaseProRecordMapper.selectRecords(timeLast23,time14,time23,query.getSupplierId(),1);
                tPurchaseProSubRecords = tPurchaseProRecordMapper.selectRecords(timeLast23,time14,time23,query.getSupplierId(),0);
            }else if(new Date().after(time20) && new Date().before(time23)) {
                tPurchaseProRecords = tPurchaseProRecordMapper.selectRecords(timeLast23,time20,time23,query.getSupplierId(),1);
                tPurchaseProSubRecords = tPurchaseProRecordMapper.selectRecords(timeLast23,time20,time23,query.getSupplierId(),0);
            }else{
                return tPurchaseProSubRecords;
            }
            if(ListUtil.isNotEmpty(tPurchaseProRecords) && ListUtil.isNotEmpty(tPurchaseProSubRecords)){
                for(TPurchaseProRecord item : tPurchaseProRecords){
                    for(TPurchaseProRecord subItem : tPurchaseProSubRecords) {
                        if(item.getSkuId().equals(subItem.getSkuId())){
                            item.setPurchaseVolume(item.getPurchaseVolume() - subItem.getPurchaseVolume());
                            if(item.getPurchaseVolume().equals(0)){
                                tPurchaseProRecords.remove(item);
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return tPurchaseProRecords;
    }

}
